<?php
require_once 'OAuth.php';

/**
 * Services_Gumi
 *
 * <code>
 * <?php
 * require_once 'Services/Gumi.php';
 *
 * $developer_key = 'xxxx';
 * $developer_secret = 'xxxx';
 *
 * $gu3api = new Services_Gumi($developer_key, $developer_secret);
 * $person = $gu3api->getPersonData(1, 'json');
 * $friends = $gu3api->getFriendData(3));
 * print_r($person);
 * ?>
 * </code>
 */
class Services_Gumi {
    protected $consumer_key;
    protected $consumer_secret;
    static public $uri = 'http://api.gu3.jp/v1/';

    public function __construct($key, $secret)
    {
        $this->consumer_key = $key;
        $this->consumer_secret = $secret;
    }

    protected function make_http_request($url, $postdata="", $headers="")
    {
        $ch = curl_init($url);
        if ($headers) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }

        if ($postdata) {
            $fields_string = "";
            foreach ($postdata as $key=>$value) {
                $fields_string .= $key . '=' . $value . '&';
            }
            rtrim($fields_string, '&');
        
            curl_setopt($ch, CURLOPT_POST, count($postdata));
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
        }

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        $result = curl_exec($ch);
        if ($result === false) {
            return curl_error($ch);
        }
        curl_close($ch);
        return $result;
    }

    protected function oauth_request($method, $url, $data=NULL)
    {
        $consumer = new OAuthConsumer($this->consumer_key,
                                      $this->consumer_secret);
        $signature_method_hmac_sha1 = new OAuthSignatureMethod_HMAC_SHA1();

        // access protected resources
        $oauth_request = OAuthRequest::from_consumer_and_token($consumer,
                                                               NULL,
                                                               $method,
                                                               $url);
        $oauth_request->sign_request($signature_method_hmac_sha1,
                                     $consumer, '');
        $headers = array($oauth_request->to_header());

        return $this->make_http_request($url, $data, $headers);
    }

    public function getPersonData($uid, $format="json") {
        if (!$uid) return false;

        $url = self::$uri . 'people/' . $uid . '/@self?format=' . $format;
        print $url;
        $json = $this->make_http_request($url);
        $obj = json_decode($json, true);

        if (!is_array($obj)) return false;
        return $obj;
    }

    public function getFriendData($uid, $format='json') {
        if (!$uid) return false;

        $url = self::$uri . 'people/' . $uid . '/@friends?format=' . $format;
        $json = $this->make_http_request($url);
        $obj = json_decode($json, true);

        if (!is_array($obj)) return false;
        return $obj;
    }

    public function postActivity($uid, $message) {
        if (!$uid || !$message) return false;

        $url = self::$uri . 'activity/' . $uid . '/';
        $data = array("message" => $message);
        $json = $this->oauth_request('POST', $url, $data);
        $obj = json_decode($json, true);

        if (!is_array($obj)) return false;
        return $obj;
    }

    public function sendMiniMail($title, $body, $toIds, $at=NULL) {
        if (!$title || !$body || !$toIds) return false;

        $url = self::$uri . 'message/sendMiniMails';
        $data = array("title" => $title,
                      "body"  => $body,
                      "toIds" => $toIds);

        if ($at) $data['at'] = $at;

        $json = $this->oauth_request('POST', $url, $data);
        $obj = json_decode($json, true);

        if (!is_array($obj)) return false;
        return $obj;
    }

    public function testAuth($message=NULL) {
        $url = self::$uri . 'test/auth';
        if ($message != NULL) {
            $url .= '?message=' . urlencode($message);
        }
        $json = $this->oauth_request('GET', $url);
        $obj = json_decode($json, true);

        if (!is_array($obj)) return false;
        return $obj;
    }
}
